<template>
	<simple-layout
		ref="simpleLayoutRef"
		:inputSearch="inputSearch"
		:scrollParams="scrollParams"
		:scrollArguments="scrollArguments"
		:listDataHandler="listDataHandler"
	/>
</template>

<script>
import simpleLayout from '@/components/simpleLayout.vue';
import {EntityTypes} from '@/utils/orgConstants';
import {getSelectKey} from '@/utils/utils';

export default {
	name: 'JoinAccount',
	components: {
		simpleLayout
	},
	inject: ['app'],
	data() {
		return {
			inputSearch: {
				placeText: this.$i18n('selectPeople.input',this.$i18n('org.exchangeAccount.label'))
			},
			scrollParams: {
				panel: 'JoinAccount',
				command: 'accountList'
			},
			scrollArguments: {}

		};
	},
	computed: {
		orgSelect() {
			return this.app().orgSelect;
		}
	},
	methods: {
		onChangeAccount(data) {
			this.$refs.simpleLayoutRef.onChangeAccount(data);
		},
		listDataHandler(data) {
			return data.map((item) => {
				const { type, name, sourcePosts } = item;
				item.$_showLabel = name;
				if (sourcePosts && sourcePosts.length) {
					let postList = [];
					sourcePosts.forEach((el) => {
						postList.push(el.name);
					});
					item.$_postName = postList.join(',');
				} else if (type === EntityTypes.Department) {
					let deptFullName = item.deptFullName;
					let names = deptFullName.split('/');
					if (names.length <= 1) {
						item.$_postName = `${deptFullName}`;
					} else {
						if (names.length > 2) {
							item.$_postName = `${names[names.length - 2]}`;
						} else {
							item.$_postName = `${item.accountShortName}`;
						}
					}
				}
				item.$_selectKey = getSelectKey(item);
				return item;
			});
		}
	},
	created() {
		let { isNeedCheckLevelScope } = this.orgSelect;
		this.scrollArguments = {
			isNeedCheckLevelScope
		};
	}
};
</script>

<style scoped lang="scss">
</style>
